'use strict';

var util = require('util');
var yeoman = require('yeoman-generator');
var path = require('path');

var Generator = module.exports = function Generator(args, options){
    yeoman.generators.Base.apply(this, arguments);
    this.argument('appname', { type: String, required: false });
    this.appname = this.appname || path.basename(process.cwd());
    this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname)));
    //把一些需要保存的数据,存在
    if(typeof this.env.options.appname == 'undefined'){
        this.env.options.appname = this.appname;
    }
    //最后执行bower 命令安装需要库文件
    this.on('end', function(){
        process.chdir('js');
        this.spawnCommand('bower',['install']);
        process.chdir('../');
    });
};

util.inherits(Generator, yeoman.generators.Base);

Generator.prototype.askFor = function askFor(){
    var cb = this.async();
    var prompts = [{
        type: 'input',
        name: 'appname',
        message: '请输入应用名',
        default: this.appname
    },{
        type: 'input',
        name: 'default_controller',
        message: '默认控制器的名字',
        default: 'IndexCtrl'
    }];
    this.prompt(prompts,function(answer){
        this.appname = answer.appname;
        this.default_controller = answer.default_controller;
        cb();
    }.bind(this));
};

Generator.prototype.projectfiles = function projectfiles(){
    this.mkdir('js');
    this.mkdir('js/controllers');
    this.mkdir('js/views');
    this.mkdir('js/directives');
    this.mkdir('js/services');
    this.mkdir('js/filters');
    this.copy('_bower.json', 'js/bower.json');
    this.copy('_view_index.html', 'js/views/index.html');
    this.template('_main.js', 'js/main.js', {appname: this.appname});
    this.template('_app.js', 'js/app.js', {appname: this.appname});
    this.template('_route.js', 'js/route.js', {controller: this.default_controller});
    this.template('controllers/_controllers.js', 'js/controllers/controllers.js', {appname: this.appname});
    this.template('controllers/_index.js', 'js/controllers/index.js', {controller: './'+this.default_controller});
    this.template('controllers/_controller.js', 'js/controllers/'+this.default_controller+'.js', {controller: this.default_controller});
    this.copy('_index.html','index.html');
};